package com.sjz.myfrist;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;


/***
 * Jsoup是一款Java的html解析工具。使用jsoup可以爬取网页的数据，如图片、文字、视频、音乐等，像python的爬虫一样，jsoup是java的爬虫。
 * 需要引入对应的包
 */
public class JsoupActivity1 extends AppCompatActivity {
    private Button jsoup1_id;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jsoup1);
        jsoup1_id = findViewById(R.id.jsoup1_id);
        imageView=findViewById(R.id.jsoup_image_1);
        
        jsoup1_id.setOnClickListener(new View.OnClickListener() {
            String string="";
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Document document = null;
                        String url = "https://blog.csdn.net/Tir_zhang?type=blog";
                        Elements elements = null;
                        try {
                            document = Jsoup.connect(url).get();
                            elements = document.getElementsByClass("blog-img-box");
                            string = elements.get(5).select("img").attr("src");
                            Log.d("TTTT", string);
                        } catch (IOException exception) {
                            exception.printStackTrace();
                        }
                    }
                }).start();
                //加载网络图片  需要先引入对应的包
               Glide.with(JsoupActivity1.this).load(string).into(imageView);
            }
        });
    }



}